

import sys
import traceback


from PyQt4.QtGui import *

from dexen.system.common import service
from dexen.system.common.states import JobState
from dexen.system.client.proxy import ServerProxy, JobProxy

from dexen.system.client import ui
from dexen.system.client.ui.job_main import JobWidget
from dexen.system.client.ui.main_widget import MainWidget
from dexen.system.client.ui.conn_widget import ConnectionWidget

# globally used by all the modules in gui
svr_proxy = ui.svr_proxy

APP_TITLE = "DEXEN-CLIENT"


class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self._main_widget = MainWidget()
        self.setCentralWidget(self._main_widget)
        self.setGeometry(200, 200, 800, 600)
        self.setWindowTitle(APP_TITLE)
        #self._create_actions()
        #self._create_menus()
        #self._create_tool_bars()
    
    def _create_actions(self):
        self.snj_act = QAction("Start a new job", self)

    def _create_menus(self):
        file_menu = self.menuBar().addMenu(QString("&File"))
        file_menu.addAction(self.snj_act)
    
    def _create_tool_bars(self):
        self.toolbar = QToolBar(self) 
        self.addToolBar(self.toolbar)
        self.toolbar.addAction(self.snj_act)
        self.toolbar.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
        self.toolbar.addSeparator()
        self.toolbar.addWidget(self.conn_widget)
        
    def show(self):
        QMainWindow.show(self)
        #dlg = ServerConnectDialog(self)
        #dlg.setModal(True)        
        #result = dlg.exec_()
        #assert result == QDialog.Accepted
        #self.job_page = MainWidget.instance()
        #assert isinstance(job_page, MainWidget)


def main():
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

"""
class Singleton(object):
    _instance = None

    @classmethod
    def instance(cls):
        if cls._instance is None:
            cls._instance = cls()
        return cls._instance
"""
"""
class ServerConnectDialog(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self, parent)
        self.setWindowTitle(APP_TITLE)
        self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
        layout = QVBoxLayout()
        self.setLayout(layout)
        h_layout = QHBoxLayout()
        self._label = QLabel("Server Addr (Ip or Name):")
        self._line_edit = QLineEdit(service.get_my_ip())
        #self._line_edit.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
        self._button = QPushButton("Connect")
        self._button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        h_layout.addWidget(self._label)
        h_layout.addWidget(self._line_edit)
        layout.addLayout(h_layout)
        layout.addWidget(self._button)
        layout.setSizeConstraint(QLayout.SetFixedSize)
        #self.setFixedSize( self.sizeHint() )
        #QtCore.QMetaObject.connectSlotsByName(self)
        self.adjustSize()
        QObject.connect(self._button, SIGNAL("clicked()"),                               
                        self, SLOT("on_button_clicked()"))
        
    @pyqtSlot()
    def on_button_clicked(self):
        try:
            self.setCursor(QCursor(Qt.WaitCursor))
            svr_proxy.connect(str(self._line_edit.text()))
        except:
            traceback.print_tb(sys.exc_info()[2])
            print "connection is failed"
            msg_box = QMessageBox(self)
            msg = "Cannot access the dexen server.\n"
            msg += "Make sure the address is valid and the server is running." 
            msg_box.setText(msg)
            msg_box.exec_()
        else:
            #self._button.setEnabled(False)
            self.accept()
            #self.emit(SIGNAL("server_connected()"))
        finally:
            self.setCursor(QCursor(Qt.ArrowCursor))
"""
